<template>
    <div class="sifter-box flex-row-center" v-if="isEdit==0">
        <el-form :inline="true" :model="sifterForm" class="demo-form-inline">
            <el-form-item label="筛选：">
                <el-input v-model="sifterForm.key" placeholder="输入人才关键字" @input="proxy.$utils.debounce(updalist,sifterForm)"></el-input>
            </el-form-item>
            <el-form-item style="width:100px;" label="">
                <el-select v-model="sifterForm.sex" placeholder="性别" clearable @change="(e)=>{typeChange(e,'sex')}">
                    <el-option v-for="item in globalData.sexAry" :key="item.value" :label="item.text" :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item style="width:140px;" label="">
                <el-select v-model="sifterForm.edu" placeholder="学历" clearable @change="(e)=>{typeChange(e,'edu')}">
                    <el-option v-for="item in globalData.eduList" :key="item.value" :label="item.text" :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item style="width:140px;" label="">
                <el-select v-model="sifterForm.salary" placeholder="薪资" clearable @change="(e)=>{typeChange(e,'salary')}">
                    <el-option v-for="item in globalData.salaryAry" :key="item.value" :label="item.text" :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item style="width:140px;" label="">
                <el-select v-model="sifterForm.workyear" placeholder="工作年限" clearable @change="(e)=>{typeChange(e,'workyear')}">
                    <el-option v-for="item in globalData.workyearAry" :key="item.value" :label="item.text" :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <div>
        </div>
    </div>

    <div class="listDiv" v-if="isEdit==0">
        <el-table :data="list" stripe style="width: 100%">
            <el-table-column prop="name" label="姓名"> </el-table-column>
            <el-table-column prop="sex" label="性别">
                <template #default="props">
                    <span>{{proxy.$utils.findObj(props.row.sex,globalData.sexAry,'value').text}}</span>
                </template>
            </el-table-column>
            <el-table-column prop="age" label="年龄"></el-table-column>
            <el-table-column prop="major" label="专业"></el-table-column>
            <el-table-column prop="edu" label="学历">
                <template #default="props">
                    <span>{{props.row.edu?proxy.$utils.findObj(props.row.edu,globalData.eduList,'value').text:'--'}}</span>
                </template>
            </el-table-column>
            <el-table-column prop="" label="操作">
                <template #default="props">
                    <!-- <span class="pointer mr10 base-color" @click="sendinterview(props.row)">邀请面试</span> -->
                    <span class="pointer mr10 base-color" @click="editData(props.row,3)">查看简历</span>
                    <span class="pointer base-color" @click="persionsave(props.row.persion_id,props.row.save)">{{props.row.save=='1'?'取消收藏':'收藏'}}</span>
                </template>
            </el-table-column>
        </el-table>
        <div class="pd20 w100 flex-row-center ju-flex-end">
            <el-pagination
                v-model:currentPage="sifterForm.currentpage"
                v-model:page-size="sifterForm.pagesize"
                :page-sizes="[10, 20, 50, 100]" :total="total"
                @size-change="()=>{updalist()}"
                @current-change="()=>{updalist()}"
                layout="total, sizes, prev, pager, next, jumper"  background/>
        </div>
    </div>

    <div class="edit-box" v-if="isEdit!=0">
        <div class="title-box flex-row-center">
            <span class="pointer return flex-row-center mr10" @click="re_turn"><el-icon><back /></el-icon></span>
            <span v-if="isEdit==3">人才简历</span>
            <el-select class="ml20 mr10" v-model="applyForm.job_id" placeholder="请选择职位" clearable @change="typeChange">
                <el-option v-for="item in recruitList" :key="item.job_id" :label="item.name" :value="item.job_id">
                </el-option>
            </el-select>
            <el-button type="primary" plain :loading="false" @click.prevent="sendinterview()">邀请面试</el-button>
        </div>
        <div class="form-box">
            <div v-if="isEdit==3" class="">
                <Resume :value="form"/>
            </div>
        </div>
    </div>

</template>

<script setup>
    import { reactive,ref,getCurrentInstance,toRefs,toRaw } from 'vue'
    import { Back,Edit,Delete } from '@element-plus/icons-vue'
    import Resume from '@/components/resume.vue'

    let { proxy } = getCurrentInstance()
    let obj = reactive({
        isEdit: 0,
        total: 0,
        list:[],
        sifterForm: {
            "key":"",
            "sex":"",
            "edu":"",
            salary:'',
            "salary_start":"",
            "salary_end":"",
            workyear: '',
            "workyear_start":"",
            "workyear_end":"",
            currentpage: 1,
            pagesize: 10
        },
        form: {},
        recruitList: [],
        applyForm:{
            "job_id": "",
            "persion_id": ""
        }
    });

    const updalist = (form)=>{
        let param = {
            ...obj.sifterForm,
            ...form
        }
        proxy.$ajax({url: 'persoin/list',data: param,bfLoading: true}).then(d => {
            console.log(d);
            if(d.success){
                total.value = d.msg.count||0
                list.value = d.msg.list;
            }else{}
        }).catch(e => {
            console.log(e)
            if(e == "登录已过期，请重新登录"){
                proxy.routerPushMixin('/')
            }
        })
    }
    updalist();

    const getrecruit = (form)=>{
        proxy.$ajax({url: 'recruit/list',data: {},bfLoading: true}).then(d => {
            console.log(d);
            if(d.success){
                recruitList.value = d.msg.list;
            }else{}
        }).catch(e => {
            if(e == "登录已过期，请重新登录"){
                proxy.routerPushMixin('/')
            }
        })
    }

    const typeChange = (e,key)=>{
        console.log(key);
        if(key=='salary'||key=='workyear'){
            sifterForm.value[`${key}_start`] = e?e[0]:''
            sifterForm.value[`${key}_end`] = e?e[1]:''
            updalist(obj.sifterForm)
        }else{
            sifterForm.value[key] = e||''
            updalist(obj.sifterForm)
        }
    }

    const re_turn = ()=>{
        isEdit.value = 0;
    }

    const editData = (item,isEditidx)=>{
        // return
        applyForm.value.persion_id = item.persion_id;
        proxy.$ajax({url: 'persion/get',data:{persion_id:item.persion_id},bfLoading: true}).then(d => {
            console.log(d);
            if(d.success){
                form.value = {
                    ...d.msg,
                    persion_id: item.persion_id
                }
                isEdit.value = isEditidx
                getrecruit()
            }else{}
        }).catch(e => {
            console.log(e)
            if(e == "登录已过期，请重新登录"){
                proxy.routerPushMixin('/')
            }
        })
    }

    // 邀请面试
    const sendinterview = ()=>{
        if(!applyForm.value.job_id){
            proxy.elMessageMixin('请选择职位', 'error')
            return
        }
        proxy.$ajax({url: 'persion/apply',data:applyForm.value,bfLoading: true}).then(d => {
            console.log(d);
            if(d.success){
                proxy.elMessageMixin('邀请成功！', 'success')
            }else{proxy.elMessageMixin(date.err, 'error')}
        }).catch(e => {
            console.log(e)
            if(e == "登录已过期，请重新登录"){
                proxy.routerPushMixin('/')
            }
        })
    }

    const persionsave = (id,save)=>{
        proxy.$ajax({url: 'persion/save',data:{persion_id:id},bfLoading: true}).then(d => {
            console.log(d);
            if(d.success){
                proxy.elMessageMixin(save==1?'取消收藏成功！':'收藏成功！', 'success')
                updalist();
            }else{proxy.elMessageMixin(date.err, 'error')}
        }).catch(e => {
            console.log(e)
            if(e == "登录已过期，请重新登录"){
                proxy.routerPushMixin('/') 
            }
        })
    }

    let { list, sifterForm, isEdit, total, form, recruitList, applyForm } = toRefs(obj)
</script>

<style lang="scss" scoped>
</style>